import sys

import facade
import parsel
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "sublibbook", "download", "pages")


class ParaBookList(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(BaseDir.get_new_path(curPath, "db.ini"),
                                             "db",
                                             self.logger)

    def select(self):
        Listpara = []
        for filepath in BaseDir.get_dir_all_files(sPath):
            filename = BaseDir.get_filename_not_extsep(filepath)
            sublibID, pages = filename.split("_")
            html = BaseDir.single_read_file(filepath)
            select = parsel.Selector(text=html)
            div_list = select.xpath('.//div[@class="searListConOut"]/div[1]/div')
            for div_tag in div_list:
                cover = div_tag.xpath('./div/div[1]/a/img/@src').get()
                url = div_tag.xpath('./div/div[2]/h4/a/@href').get()
                title = div_tag.xpath('./div/div[2]/h4/a/text()').get()
                Listpara.append((url, cover, title, sublibID, pages))
        sql = "insert ignore into books (url,cover,title,sublibID,pages) values (%s,%s,%s,%s,%s)"
        self.mysqlutils.ExeSqlMany(sql, Listpara)

    def test_is_keyword(self):
        """
        测试是否所有关键词都为空
        测试结果为全部为空 没有关键词
        :return:
        """
        for filepath in BaseDir.get_dir_all_files(sPath):
            html = BaseDir.single_read_file(filepath)
            select = parsel.Selector(text=html)
            div_list = select.xpath('.//div[@class="searListConOut"]/div[1]/div')
            for div_tag in div_list:
                title = div_tag.xpath(
                    './div/div[2]/div[1]/div[3]//text()').getall()
                title = "".join(title)
                title = title.replace("关键词：", "").strip()
                if title:
                    print(title)
                    sys.exit(-1)


if __name__ == "__main__":
    para = ParaBookList()
    para.select()
